<?php
/**
 * 应用程序
 * @author zengnz
 *
 */
class AppAction extends CommonAction
{

	/**
	 * 首页
	 */
	public function index() {
		
		$this -> assign('action', 'app');
		$this -> display();
	}
	
	/**
	 * 我的车库
	 */
	public function garage() {
		
		$this -> display();
	}
	
	/**
	 * 微信接口
	 */
	public function fetion() {
		
		$this -> display();
	} 
	
	
	/**
	 *  站内互动
	 */
	public function interactive() {
		
		$this -> display();
	}
	
	/**
	 * 交友
	 */
	public function makeFriend() {
		$cId = $this -> getParam('id');
		$UserLogic = D('User', 'Logic');
		if ($cId) {
			$uId = $this -> getUid();
			$uAccount = $this -> getCustomer();
			$cInfo = $UserLogic -> getUserBaseInfo($cId);
			
			$data = array(
					'f_id' 		 => $uId,
					'f_account'  => $uAccount,
					'to_id'		 => $cId,
					'to_account' => $cInfo['account'],
					'apply_time' => time()
			);
				
			$replyData = array_merge($data, array('status'=>0));
				
			$Model = new Model();
			$Model -> startTrans();
			
			$repRet = D('FriendReplyList') -> add($replyData);
			
			if ($repRet) {
				$applyData = array_merge($data, array('status'=>1));
			} else {
				$applyData = array_merge($data, array('status'=>0));
			}
			$ret = D('FriendApplyList') -> add($applyData);
			
			if (!$ret) {
				$Model -> rollback();
				$this -> error('申请好友失败！');
			} else {	
				$Model -> commit();
			}	
			
			$this -> redirect('friendList');	
		} else {
			$list = $UserLogic -> getOpenUser();
			$uId = $this -> getUid();
			$userList = array();
			// 排除自己和已加好友
			if (is_array($list) && !empty($list)) {
				foreach ($list as $key => $val) {
					if ($val['id'] != $uId)
						$userList[] = $val;
				}
			}
			
			$this -> assign('userList', $userList);
			$this -> display();
		}
	}
	
	public function friendList() {
		echo "我的好友";exit;
	}
	
}